//
//  ViewController.m
//  orderMeal
//
//  Created by Davin on 14-12-7.
//  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIWebViewDelegate>{
    UIWebView *webView;
}

@property(nonatomic, retain) IBOutlet UIWebView *webView;

@end

@implementation ViewController
@synthesize webView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *user = self.getUserName;
    NSString *pass = self.getPassword;
    
    if (self.getUserName == nil) {
        
    }else{
        //存在用户信息
        [self showHomeView:user:pass];
//        NSString *idfv = [[[UIDevice currentDevice]identifierForVendor] UUIDString];
    
    }
    
//    NSLog(@"didFailLoadWithError:%@", idfv );
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [activityIndicator stopAnimating];
    UIView *view = (UIView*)[self.view viewWithTag:108];
    [view removeFromSuperview];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"didFailLoadWithError:%@", error);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSString*)getUserName
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults stringForKey:@"username"];
}
- (NSString*)getPassword
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults stringForKey:@"password"];
}

- (void)showHomeView:(NSString *)username :(NSString *)password
{
    NSString * homeURL = [NSString stringWithFormat:@"http://ordermeal.loc/mobile/login?username=%@&password=%@", username, password];
    NSLog(@"open web site:%@", homeURL);
    NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:homeURL]];
    
    [self.view addSubview: webView];
    [webView loadRequest:request];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [view setTag:108];
    [view setBackgroundColor:[UIColor blackColor]];
    [view setAlpha:0.5];
    [self.view addSubview:view];
    
    activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [activityIndicator setCenter:view.center];
    [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:activityIndicator];
    [activityIndicator startAnimating];
}

@end
